import { goods_list } from '@/api/class'
import { type listQueryInterface } from '@/types/class'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
export default function () {
  let queryForm = reactive<listQueryInterface>({
    page: 1,
    list_rows: 10,
    keyword: '',
    cate_id: '',
    total: 0
  })
  let list = reactive<any>([])
  let responseData = reactive({})
  const loadList = (options?: any) => {
    Object.assign(queryForm, options)
    goods_list(queryForm).then((res: any) => {
      queryForm.total = res.total
      list.splice(0, list.length)
      list = Object.assign(list, res.data)
      Object.assign(responseData, res)
    })
  }

  const changeQuery = (e: any) => {
    if (e.type == 'page') {
      queryForm.page = e.value
      loadList()
    } else if (e.type == 'pageSize') {
      queryForm.list_rows = e.value
      queryForm.page = 1
      loadList()
    }
  }

  const router = useRouter()
  const navToGood = (options: any) => {
    router.push({ path: '/goods', query: options })
  }
  return {
    queryForm,
    list,
    responseData,
    loadList,
    navToGood,
    changeQuery
  }
}
